/*
 * @Descripttion: 
 * @version: 
 * @Author: ada.wu
 * @Date: 2024-01-05 10:17:42
 * @LastEditors: ada.wu
 * @LastEditTime: 2024-04-07 10:26:08
 */
import {Module} from 'vuex';
// 此处加上 `.ts` 后缀报错，具体原因不详
import {RootStateTypes, MapsState} from '../interface';
import useMapEffect from "/@/hooks/useMapEffect";

const maps = useMapEffect()
const usersModule: Module<MapsState, RootStateTypes> = {
    namespaced: true,
    state: {
        location: {
            lng: '',
            lat: '',
            address: '',
            adcode: '',
        }
    },
    mutations: {
        // 设置用户信息
        setLocation(state: any, location: any) {
            state.location = {...location}
        },
    },
    actions: {
        getLocation({commit, state}) {
            if (state.location.lat) {
                return Promise.resolve(state.location)
            }
            return new Promise((resolve, reject) => {
                maps.getLocation().then(location => {
                    commit('setLocation', location)
                    resolve(location)
                }).catch(reject)
            })

        }
    },
};

export default usersModule;
